package com.imageim.yimirror.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.bean.PersonalReportBean;
import com.imageim.yimirror.common.ui.GlideUtils;
import com.imageim.yimirror.common.ui.dialog.PromptDialog;
import com.imageim.yimirror.common.util.StyleUtil;
import com.martin.ads.omoshiroilib.util.BitmapUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Created by Yuaihen.
 * on 2018/12/4
 */
public class PersonalReportWomenAdapter extends RecyclerView.Adapter {


    private Context context;
    //风格定位
    private static final int TYPE_STYLE_POSITIONING = 0;
    //色调定位
    private static final int TYPE_COLOR_STYLE = 1;
    //穿衣指南
    private static final int TYPE_DRESS_GUIDE = 2;
    //适合廓形
    private static final int TYPE_SUIT_FITOUTLINE = 3;
    //服装类型
    private static final int TYPE_CLOTHING_TYPE = 4;
    private PersonalReportBean.ListBean mData;
    private PromptDialog mDialog;

    public PersonalReportWomenAdapter(Context context) {
        this.context = context;
    }

    public void setData(PersonalReportBean.ListBean data) {
        if (data != null) {
            mData = data;
            notifyDataSetChanged();
        }
    }


    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_STYLE_POSITIONING;
            case 1:
                return TYPE_COLOR_STYLE;
            case 2:
                return TYPE_DRESS_GUIDE;
            case 3:
                return TYPE_SUIT_FITOUTLINE;
            case 4:
                return TYPE_CLOTHING_TYPE;
            default:
                return 0;
        }

    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : 5;
    }

    /**
     * 根据ViewType创建不同的布局
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_STYLE_POSITIONING:
                View stylePositionView = LayoutInflater.from(context).inflate(R.layout.personal_recycle_item_style, parent, false);
                return new StylePositionHolder(stylePositionView);
            case TYPE_COLOR_STYLE:
                View styleView = LayoutInflater.from(context).inflate(R.layout.personal_recycle_item_color, parent, false);
                return new ColorHolder(styleView);
            case TYPE_DRESS_GUIDE:
                View colorStyleView = LayoutInflater.from(context).inflate(R.layout.personal_recycle_item_dressguide, parent, false);
                return new DressGuideHolder(colorStyleView);
            case TYPE_SUIT_FITOUTLINE:
                View suitFitoutLine = LayoutInflater.from(context).inflate(R.layout.personal_recycle_item_suit_fitoutline, parent, false);
                return new SuitFitOutLineHolder(suitFitoutLine);
            case TYPE_CLOTHING_TYPE:
                View clothingTypeView = LayoutInflater.from(context).inflate(R.layout.personal_recycle_item_clothing_type, parent, false);
                return new ClothingTypeHolder(clothingTypeView);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case TYPE_STYLE_POSITIONING:
                StylePositionHolder stylePositionHolder = (StylePositionHolder) holder;
                stylePositionHolder.setData(mData);
                stylePositionHolder.mTvShare.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        openBigQrCode();
                    }
                });
                break;
            case TYPE_COLOR_STYLE:
                ColorHolder colorHolder = ((ColorHolder) holder);
                colorHolder.setData(mData);
                break;
            case TYPE_DRESS_GUIDE:
                DressGuideHolder dressGuideHolder = ((DressGuideHolder) holder);
                dressGuideHolder.setData(mData);
                break;
            case TYPE_SUIT_FITOUTLINE:
                SuitFitOutLineHolder suitFitOutLineHolder = ((SuitFitOutLineHolder) holder);
                suitFitOutLineHolder.setData(mData);
                break;
            case TYPE_CLOTHING_TYPE:
                ClothingTypeHolder clothingTypeHolder = ((ClothingTypeHolder) holder);
                clothingTypeHolder.setData(mData);
                break;
            default:
        }
    }


    /**
     * 点击二维码弹开的Dialog
     */
    public void openBigQrCode() {
        if (mData != null) {
            String qrCode = mData.getQrCode();
            if (!TextUtils.isEmpty(qrCode)) {
                if (mDialog == null) {
                    mDialog = new PromptDialog(context);
                    mDialog.setOutsideCanelable(true);
                    mDialog.bindView(R.layout.dialog_big_qrcode, new PromptDialog.BindViewListener() {
                        @Override
                        public void onBindView(View contentView) {
                            ImageView mIvBigQrcode = contentView.findViewById(R.id.iv_bigQrCode);
                            mIvBigQrcode.setImageBitmap(BitmapUtils.getQrcodeBitmapWithPadding(qrCode, 300, Color.BLACK, 5));
                        }
                    }).show();
                } else {
                    mDialog.show();
                }
            }
        }

    }


    static class StylePositionHolder extends RecyclerView.ViewHolder {
        private TextView mTvStyleName1, mTvStyleName2, mTvStyleName3, mTvStyleDesc1, mTvStyleDesc2, mTvStyleDesc3, mTvShare;
        private TextView[] mTvName;
        private TextView[] mTvDesc;

        public StylePositionHolder(View itemView) {
            super(itemView);
            mTvStyleName1 = itemView.findViewById(R.id.tv_style_name1);
            mTvStyleName2 = itemView.findViewById(R.id.tv_style_name2);
            mTvStyleName3 = itemView.findViewById(R.id.tv_style_name3);
            mTvStyleDesc1 = itemView.findViewById(R.id.tv_style_desc1);
            mTvStyleDesc2 = itemView.findViewById(R.id.tv_style_desc2);
            mTvStyleDesc3 = itemView.findViewById(R.id.tv_style_desc3);
            mTvShare = itemView.findViewById(R.id.tv_share);

            mTvName = new TextView[]{mTvStyleName1, mTvStyleName2, mTvStyleName3};
            mTvDesc = new TextView[]{mTvStyleDesc1, mTvStyleDesc2, mTvStyleDesc3};
        }

        public void setData(PersonalReportBean.ListBean data) {
            List<PersonalReportBean.ListBean.StyleListBean> styleList = Optional.ofNullable(data)
                    .flatMap(b -> Optional.ofNullable(b.getStyleList()))
                    .orElse(new ArrayList<>());

            for (int i = 0; i < styleList.size(); i++) {
                if (i >= mTvName.length) {
                    return;
                }

                PersonalReportBean.ListBean.StyleListBean bean = styleList.get(i);
                if (bean != null) {
                    mTvName[i].setText(MessageFormat.format("{0} {1}%", bean.getName(), bean.getScore()));
                    mTvDesc[i].setText(bean.getDescript());
                }
            }


        }
    }


    class ColorHolder extends RecyclerView.ViewHolder {
        ImageView ivColorPic;
        TextView tvColorDesc;

        ColorHolder(View itemView) {
            super(itemView);
            ivColorPic = itemView.findViewById(R.id.personal_iv_color);
            tvColorDesc = itemView.findViewById(R.id.personal_tv_color_desc);

        }

        public void setData(PersonalReportBean.ListBean data) {
            if (data != null) {
                tvColorDesc.setText(data.getToneDecript());
                ivColorPic.setBackgroundResource(StyleUtil.getColorImageForId(data.getStyleId()));
            }
        }
    }

    class DressGuideHolder extends RecyclerView.ViewHolder {

        private TextView mTvDressGuideDesc1;
        private TextView mTvDressGuideDesc2;
        private ImageView mIvDressGuidePic1;
        private ImageView mIvDressGuidePic2;

        DressGuideHolder(View itemView) {
            super(itemView);
            mTvDressGuideDesc1 = itemView.findViewById(R.id.tv_dress_guide_desc1);
            mTvDressGuideDesc2 = itemView.findViewById(R.id.tv_dress_guide_desc2);
            mIvDressGuidePic1 = itemView.findViewById(R.id.iv_dress_guide_pic1);
            mIvDressGuidePic2 = itemView.findViewById(R.id.iv_dress_guide_pic2);
        }

        public void setData(PersonalReportBean.ListBean data) {
            if (data != null) {
                PersonalReportBean.ListBean.ColorCollocationBean colorCollocation = data.getColorCollocation();
                mTvDressGuideDesc1.setText(colorCollocation.getLightnessDecript());
                mTvDressGuideDesc2.setText(colorCollocation.getPurityDecript());

                mIvDressGuidePic1.setBackgroundResource(StyleUtil.getDressIconOneByStyleId(data.getStyleId()));
                mIvDressGuidePic2.setBackgroundResource(StyleUtil.getDressIconTwoByStyleId(data.getStyleId()));
            }
        }
    }


    class SuitFitOutLineHolder extends RecyclerView.ViewHolder {

        private final ImageView mIvPic1;
        private final ImageView mIvPic2;
        private final ImageView mIvPic3;
        private ImageView[] picArrays;

        SuitFitOutLineHolder(View itemView) {
            super(itemView);
            mIvPic1 = itemView.findViewById(R.id.iv_pic1);
            mIvPic2 = itemView.findViewById(R.id.iv_pic2);
            mIvPic3 = itemView.findViewById(R.id.iv_pic3);

            picArrays = new ImageView[]{mIvPic1, mIvPic2, mIvPic3};
        }

        public void setData(PersonalReportBean.ListBean data) {
            String profiles = data.getProfiles();
            if (!TextUtils.isEmpty(profiles)) {
                String[] profilesArray = profiles.split(",");
                for (int i = 0; i < profiles.length(); i++) {
                    if (i >= picArrays.length || i >= profilesArray.length) {
                        return;
                    }

                    GlideUtils.display(picArrays[i], profilesArray[i]);
                }
            }
        }
    }


    class ClothingTypeHolder extends RecyclerView.ViewHolder {

        private PersonalReportGridViewAdapter mPersonalReportGridViewAdapter;

        ClothingTypeHolder(View itemView) {
            super(itemView);
            GridView gridView = itemView.findViewById(R.id.gridview_personal);
            mPersonalReportGridViewAdapter = new PersonalReportGridViewAdapter(context);
            gridView.setAdapter(mPersonalReportGridViewAdapter);

        }

        public void setData(PersonalReportBean.ListBean data) {
            if (data != null) {
                mPersonalReportGridViewAdapter.setData(mData, 10);
            }

        }
    }
}
